package com.vegetable.modules.controller.baseData;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.ProductCategory;
import com.vegetable.modules.form.ProductCategoryForm;
import com.vegetable.modules.query.ProductCategoryQuery;
import com.vegetable.modules.service.baseData.IProductCategoryService;
import com.vegetable.modules.vo.ProductCategoryTreeVO;
import com.vegetable.modules.vo.ProductCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-12-10
 */
@Slf4j
@RestController
@RequestMapping("/baseData/productCategory")
@Api(tags = "商品分类管理")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService productCategoryService;

    @ApiOperation("分页查询商品分类")
    @GetMapping("/page")
    public R<IPage<ProductCategoryVO>> getPageList(@Validated ProductCategoryQuery query) {
        IPage<ProductCategoryVO> pageResult = productCategoryService.getPageList(query);
        return R.data(pageResult);
    }

    @ApiOperation("获取商品分类树")
    @GetMapping("/tree")
    public R<List<ProductCategoryTreeVO>> getCategoryTree() {
        List<ProductCategoryTreeVO> tree = productCategoryService.getCategoryTree();
        return R.data(tree);
    }

    @ApiOperation("根据父分类ID获取子分类")
    @GetMapping("/children/{parentId}")
    public R<List<ProductCategory>> getChildrenByParentId(
            @ApiParam("父分类ID") @PathVariable Long parentId) {
        List<ProductCategory> children = productCategoryService.getChildrenByParentId(parentId);
        return R.data(children);
    }

    @ApiOperation("获取所有顶级分类")
    @GetMapping("/top")
    public R<List<ProductCategory>> getTopCategories() {
        List<ProductCategory> topCategories = productCategoryService.getTopCategories();
        return R.data(topCategories);
    }

    @ApiOperation("根据分类名称搜索")
    @GetMapping("/search")
    public R<List<ProductCategory>> searchByName(
            @ApiParam("分类名称") @RequestParam String categoryName) {
        List<ProductCategory> categories = productCategoryService.searchByName(categoryName);
        return R.data(categories);
    }

    @ApiOperation("获取分类详情")
    @GetMapping("/detail/{id}")
    public R<ProductCategoryVO> getCategoryDetail(
            @ApiParam("分类ID") @PathVariable Long id) {
        ProductCategoryVO detail = productCategoryService.getCategoryDetail(id);
        if (detail == null) {
            return R.fail("分类不存在");
        }
        return R.data(detail);
    }

    @ApiOperation("新增商品分类")
    @PostMapping("/add")
    public R addCategory(@Validated @RequestBody ProductCategoryForm form) {
        return productCategoryService.addCategory(form);
    }

    @ApiOperation("更新商品分类")
    @PutMapping("/update")
    public R updateCategory(@Validated @RequestBody ProductCategoryForm form) {
        return productCategoryService.updateCategory(form);
    }

    @ApiOperation("删除商品分类")
    @DeleteMapping("/delete/{id}")
    public R deleteCategory(@ApiParam("分类ID") @PathVariable Long id) {
        return productCategoryService.deleteCategory(id);
    }

    @ApiOperation("启用/禁用分类")
    @PutMapping("/enable/{id}")
    public R enableCategory(
            @ApiParam("分类ID") @PathVariable Long id,
            @ApiParam("是否启用：0否1是") @RequestParam Integer isEnabled) {
        return productCategoryService.enableCategory(id, isEnabled);
    }

    @ApiOperation("验证分类编码是否唯一")
    @GetMapping("/checkCode")
    public R<Boolean> checkCategoryCode(
            @ApiParam("分类编码") @RequestParam String categoryCode,
            @ApiParam("排除的ID") @RequestParam(required = false) Long excludeId) {
        boolean isUnique = productCategoryService.isCategoryCodeUnique(categoryCode, excludeId);
        return R.data(isUnique);
    }

    @ApiOperation("根据分类编码查询")
    @GetMapping("/getByCode")
    public R<ProductCategory> getByCategoryCode(
            @ApiParam("分类编码") @RequestParam String categoryCode) {
        ProductCategory category = productCategoryService.getByCategoryCode(categoryCode);
        return R.data(category);
    }
}